package model

import (
	"strconv"
	"time"
)

type DailyIdentification struct {
	Id int64 `xorm:"pk autoincr"`
	SalesCategory int
	Date string
	CreatedAt time.Time
	UpdatedAt time.Time
}

type Month struct {
	Name string
	WeekList []*Week
}

type Week struct {
	Name string
	DayList []*Day
}

type Day struct {
	Name string
	Hoilday bool
}

func ZellerWeek(year, month, day int) string {

	var weekday = [7]string{"周日", "周一", "周二", "周三", "周四", "周五", "周六"}

	var y, m, c int
	if month >= 3 {
		m = month
		y = year % 100
		c = year / 100
	} else {
		m = month + 12
		y = (year - 1) % 100
		c = (year - 1) / 100
	}

	week := y + (y / 4) + (c / 4) - 2*c + ((26 * (m + 1)) / 10) + day - 1
	if week < 0 {
		week = 7 - (-week)%7
	} else {
		week = week % 7
	}
	which_week := week
	return weekday[which_week]
}

func GetMonth(m string) int{
	switch m {
	case "January":
		return 1
	case "February":
		return 2
	case "March":
		return 3
	case "April":
		return 4
	case "May":
		return 5
	case "June":
		return 6
	case "July":
		return 7
	case "August":
		return 8
	case "September":
		return 9
	case "October":
		return 10
	case "November":
		return 11
	case "December":
		return 12
	}
	return 0
}

func GetBetweenDates(sdate, edate string) []string {
	d := []string{}
	timeFormatTpl := "2006-01-02 15:04:05"
	if len(timeFormatTpl) != len(sdate) {
		timeFormatTpl = timeFormatTpl[0:len(sdate)]
	}
	date, err := time.Parse(timeFormatTpl, sdate)
	if err != nil {
		// 时间解析，异常
		return d
	}
	date2, err := time.Parse(timeFormatTpl, edate)
	if err != nil {
		// 时间解析，异常
		return d
	}
	if date2.Before(date) {
		// 如果结束时间小于开始时间，异常
		return d
	}
	// 输出日期格式固定
	timeFormatTpl = "2006-01-02"
	date2Str := date2.Format(timeFormatTpl)
	d = append(d, date.Format(timeFormatTpl))
	for {
		date = date.AddDate(0, 0, 1)
		dateStr := date.Format(timeFormatTpl)
		d = append(d, dateStr)
		if dateStr == date2Str {
			break
		}
	}
	return d
}

func QueryHoilday(line int)([]*Month, error){

	var mList []*Month

	for i:=1; i<=12; i++{
		currentYear, _, _ := time.Now().Date()

		var firstOfMonth time.Time
		if i < 10{
			firstOfMonth, _ = time.Parse("2006-01-02", strconv.Itoa(currentYear)+"-0"+strconv.Itoa(i)+"-01")
		}else{
			firstOfMonth, _ = time.Parse("2006-01-02", strconv.Itoa(currentYear)+"-"+strconv.Itoa(i)+"-01")
		}

		lastOfMonth := firstOfMonth.AddDate(0, 1, -1)

		var m = Month{
			Name: lastOfMonth.Format("2006-01"),
		}

		var wList = []*Week{
			{
				Name:"周日",
			},
			{
				Name:"周一",
			},
			{
				Name:"周二",
			},
			{
				Name:"周三",
			},
			{
				Name:"周四",
			},
			{
				Name:"周五",
			},
			{
				Name:"周六",
			},
		}

		dayLsit := GetBetweenDates(firstOfMonth.Format("2006-01-02"), lastOfMonth.Format("2006-01-02"))
		for _, day := range dayLsit{

			var d = Day{
				Name: day,
			}

			var di DailyIdentification
			x.Where("sales_category = ?", line).And("date = ?", day).Get(&di)
			if di.Id != 0{
				d.Hoilday = true
			}

			t, err := time.Parse("2006-01-02", day)
			if err != nil{
				return nil, err
			}

			getWeek := ZellerWeek(t.Year(), GetMonth(t.Month().String()), t.Day())

			switch getWeek {
			case "周日":
				wList[0].DayList = append(wList[0].DayList, &d)
			case "周一":
				wList[1].DayList = append(wList[1].DayList, &d)
			case "周二":
				wList[2].DayList = append(wList[2].DayList, &d)
			case "周三":
				wList[3].DayList = append(wList[3].DayList, &d)
			case "周四":
				wList[4].DayList = append(wList[4].DayList, &d)
			case "周五":
				wList[5].DayList = append(wList[5].DayList, &d)
			case "周六":
				wList[6].DayList = append(wList[6].DayList, &d)
			}
		}

		m.WeekList = wList
		mList = append(mList, &m)
	}

	return mList, nil
}

func InsertHoilday(line int, mList []*Month)(map[string]interface{}, error){

	session := x.NewSession()

	for _, mv := range mList{
		for _, wv := range mv.WeekList{
			for _, dv := range wv.DayList{

				var di DailyIdentification
				if dv.Hoilday == true {
					_, err := session.Where("sales_category = ?", line).And("date = ?", dv).Get(&di)
					if err != nil{
						session.Rollback()
					}
					if di.Id == 0 {
						di.SalesCategory = line
						di.Date = dv.Name
						_, err := session.Insert(&di)
						if err != nil{
							session.Rollback()
						}
					}
				}else {
					_, err := session.Where("sales_category = ?", line).And("date = ?", dv).Get(&di)
					if err != nil{
						session.Rollback()
					}
					if di.Id != 0 {
						_, err := session.Delete(&di)
						if err != nil{
							session.Rollback()
						}
					}
				}
			}
		}
	}
	session.Commit()

	return map[string]interface{}{"code":200, "message":"成功"}, nil
}













































































